package com.waterfairy.note.utils;

import android.app.Activity;
import android.os.Build;
import android.text.TextUtils;
import android.webkit.WebSettings;
import android.webkit.WebView;

import java.lang.reflect.Field;

/**
 * @author water_fairy
 * @email 995637517@qq.com
 * @date 2020/8/26 14:10
 * @info:
 */
public class WebViewUtils {
    public static void init(WebView webView) {
        if (webView == null) return;
        android.webkit.WebSettings settings = webView.getSettings();
        // 设置WebView属性，能够执行Javascript脚本
        settings.setJavaScriptEnabled(true);
        settings.setDomStorageEnabled(true);

        //https  与 http  混合实用是  不显示图片
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
            webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        webView.getSettings().setBlockNetworkImage(false);
    }


    public static void releaseAllWebViewCallback() {
        try {
            Field sConfigCallback = Class.forName("android.webkit.BrowserFrame").getDeclaredField("sConfigCallback");
            if (sConfigCallback != null) {
                sConfigCallback.setAccessible(true);
                sConfigCallback.set(null, null);
            }
        } catch (NoSuchFieldException e) {
            e.printStackTrace();

        } catch (ClassNotFoundException e) {
            e.printStackTrace();

        } catch (IllegalAccessException e) {
            e.printStackTrace();

        }
    }


    /**
     * 调用Android
     *
     * @param activity
     * @param webView
     * @param commandCode
     * @param commandContent
     */
    public static void invokeAndroid(final Activity activity, final WebView webView, String commandCode, String commandContent) {
        if (activity == null || TextUtils.isEmpty(commandCode)) return;
        if (!TextUtils.isEmpty(commandCode)) {

        }
    }


    public static void load(WebView webView, String command) {
        webView.loadUrl("javascript:" + command);
    }

    public static void onResume(WebView webView) {
        if (webView != null)
            webView.loadUrl("javascript:onResume()");
    }

    public static void onPause(WebView webView) {
        if (webView != null)
            webView.loadUrl("javascript:onPause()");
    }
}
